﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CopyToFolder
{
    public partial class Form1 : Form
    {
        List<string> dirs;
        public Form1()
        {
            InitializeComponent();
            if (dirs == null)
                dirs = new List<string>();
        }

        private void buttonFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*.*)|*.*";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = fileDialog.FileName;
                labelFile.Text = file;
                //MessageBox.Show("已选择文件:" + file, "选择文件提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void buttonFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowNewFolderButton = false;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                labelFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void buttonDo_Click(object sender, EventArgs e)
        {
            string folder = labelFolder.Text;
            if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
            {
                MessageBox.Show("目录 " + folder + " 不存在", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string filePath = labelFile.Text;
            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            {
                MessageBox.Show("文件 " + filePath + " 不存在", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DoGetDirs(folder);
            DoCopy(filePath);
        }

        void DoCopy(string filePath)
        {
            string fileName = Path.GetFileName(filePath);
            foreach (var path in dirs)
            {
                string targetPath = path + "\\" + fileName;
                try
                {
                    if (File.Exists(targetPath) && !isRewrite.Checked)
                        throw new Exception("文件已经存在：" + targetPath);
                    else
                        File.Copy(filePath, targetPath, isRewrite.Checked);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            MessageBox.Show("拷贝完成", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //获取所有文件夹及子文件夹
        void DoGetDirs(string dirPath)
        {
            dirs.Clear();
            dirs.Add(dirPath);
            GetDirs(dirPath);
        }

        private void GetDirs(string dirPath)
        {
            if (Directory.GetDirectories(dirPath).Length > 0)
            {
                foreach (string path in Directory.GetDirectories(dirPath))
                {
                    dirs.Add(path);
                    GetDirs(path);
                }
            }
        }

        private void buttonAbout_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.liangshaoze.com/archives/1082");
        }
    }
}
